// var common = require('../../../utils/util.js');
let app = getApp();


Page({

  data: {
    playing: false ,
    musicList: null 
  },
  onLoad: function (options) {
    var that = this;
    this.getMyMusics();

  },

  playingtoggle: function (event) {

    var that = this ;

    this.setData({ playing: this.data.playing? false:true });


    if (this.data.playing) {

      var max = that.data.musicList.length-1;
      var min= 0 ;
      var index = Math.floor(Math.random() * (max - min + 1) + min);
      
      //第二版会升级音乐播放器。下一首 等
      var music = that.data.musicList[index];
      // 全局播放器
      wx.getBackgroundAudioManager().title = music.title ;
      wx.getBackgroundAudioManager().epname = music.epname;
      wx.getBackgroundAudioManager().singer = music.singer;
      wx.getBackgroundAudioManager().coverImgUrl = music.coverImgUrl;


      wx.getBackgroundAudioManager().src = music.src ;
      wx.getBackgroundAudioManager().play();
    } else {
      wx.getBackgroundAudioManager().pause();
    }

  },


  getMyMusics: function () {
    wx.showLoading({
      title: 'loading',
    })
    const that = this;
    wx.request({
      url: 'https://www.aivin666.cn/AivinInfo/getAllMusicInfoGson',
      data: { pageNum: 2 },
      header: {
        'Content-Type': 'application/json'
      },
      method: 'post',

      success: function (res) {

        that.setData({
          musicList: res.data
        });
        wx.hideLoading();
      }
    })
  },

  
   onShow: function (options) {
     wx.getBackgroundAudioManager().play();
  },
  onHide: function () {
    wx.getBackgroundAudioManager().pause();
  },

  
})
